package cn.kimming.service.impl;

import cn.kimming.common.exception.SystemException;
import cn.kimming.common.pojo.RoomPlan;
import cn.kimming.common.pojo.RoomType;
import cn.kimming.common.util.DateUtil;
import cn.kimming.common.vo.RoomPlanVO;
import cn.kimming.mapper.RoomPlanMapper;
import cn.kimming.mapper.RoomTypeMapper;
import cn.kimming.service.IRoomPlanService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 刘铭轩
 * date 2021-06-02
 */
@Service
@Transactional
public class RoomPlanServiceImpl implements IRoomPlanService {
    @Autowired
    private RoomPlanMapper roomPlanMapper;
    @Autowired
    private RoomTypeMapper roomTypeMapper;
    @Override
    public List<RoomPlanVO> findVOByFutureDate(String futureDate) {
        return roomPlanMapper.findVOByFutureDate(futureDate);
    }

    @Override
    public void updateDiscountAndPrice(RoomPlan bo) {
        checkFutureDate(bo.getFutureDate());
        checkRoomType(bo.getRoomTypeId());
        roomPlanMapper.updateByFutureDateAndTypeId(bo);
    }

    /**
     * 检查房型记录是否存在
     * @param roomTypeId
     */
    public void checkRoomType(Long roomTypeId) {
        RoomType roomType = roomTypeMapper.selectByPrimaryKey(roomTypeId);
        if (roomType == null) throw new SystemException("没有此房型记录");
    }

    /**
     * 判读日期是否在20天之内
     * @param futureDate
     */
    public void checkFutureDate(Date futureDate) {
        Date minDate = DateUtil.getAfterDate(0);
        Date maxDate = DateUtil.getAfterDate(19);
        if (futureDate.compareTo(minDate) < 0 || futureDate.compareTo(maxDate) > 0)
            throw new SystemException("日期必须在" + DateUtil.date2String(minDate) + "至" +DateUtil.date2String(maxDate) + "之间");

    }

    @Override
    public List<JSONObject> showTodayPrice() {
        return roomPlanMapper.showTodayPrice();
    }
}
